﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using TSB.FarmPowerShellWizard.Entities;

namespace TSB.FarmPowerShellWizard.Repository
{
    public class FarmRepository
    {
        private TSBFarm GetFarm()
        {
            var farm = new TSBFarm();

            var localFarm = SPFarm.Local;
            var centralAdminSite = SPContext.Current.Site;

            farm.DatabaseServer = centralAdminSite.ContentDatabase.Server;
            farm.DatabaseName = centralAdminSite.ContentDatabase.Name;
            farm.CentralAdminPort = centralAdminSite.Port;
            farm.AdminDatabaseName = localFarm.Name;
            farm.FarmUsername = localFarm.DefaultServiceAccount.LookupName();
            farm.Servers = new List<string>();
           
            foreach (var srv in localFarm.Servers)
            {
                if (string.IsNullOrEmpty(srv.Name) || srv.Role == SPServerRole.Invalid)
                {
                    continue;
                }
                farm.Servers.Add(srv.Name);
            }

            return farm;
        }

        private List<TSBManagedAccount> GetManagedAccounts()
        {
            var accounts = new List<TSBManagedAccount>();
            
            var a = new SPFarmManagedAccountCollection(SPFarm.Local);
            foreach (var acc in a)
            {
                accounts.Add(new TSBManagedAccount {DisplayName = acc.DisplayName});
            }

            return accounts;
        }

        public string GetServerScript()
        {
            var ps = new StringBuilder();
            var farmSettings = GetFarm();

            ps.AppendLine(Helpers.GetComment("FARM_Intro"));
            ps.AppendLine(Helpers.GetComment("FARM_SetPassphrase"));
            ps.Append("\n$farmPassword = \"[set]\"");
            ps.Append("\n$passPhrase = \"[set]\"");

            ps.AppendFormat("\n$farmUsername = \"{0}\"", farmSettings.FarmUsername);
            ps.AppendFormat("\n$databaseServer = \"{0}\"", farmSettings.DatabaseServer);
            ps.AppendFormat("\n$configDBName = \"{0}\"", farmSettings.DatabaseName);
            ps.AppendFormat("\n$adminDBName = \"{0}\"", farmSettings.AdminDatabaseName);
            ps.AppendFormat("\n$centralAdminPort = \"{0}\"", farmSettings.CentralAdminPort);

            ps.AppendLine(Helpers.GetScriptComment("FARM_FarmCredientials"));

            ps.AppendLine(Helpers.GetComment("FARM_RunOnCAServer"));
            ps.AppendLine(Helpers.GetHostComment("FARM_WH_InstallingConfigDB"));
            ps.AppendLine(Helpers.GetScriptComment("FARM_NewSPConfigDB"));
            ps.AppendLine(Helpers.GetHostComment("FARM_WH_InstallingFarm1"));
            ps.AppendLine(Helpers.GetScriptComment("FARM_SPHelpCollection"));
            ps.AppendLine(Helpers.GetScriptComment("FARM_SPResourceSecurity"));
            ps.AppendLine(Helpers.GetScriptComment("FARM_SPService"));
            ps.AppendLine(Helpers.GetHostComment("FARM_WH_InstallingCA"));
            ps.AppendLine(Helpers.GetScriptComment("FARM_NewSPCentralAdmin"));
            ps.AppendLine(Helpers.GetHostComment("FARM_WH_InstallingFarm2"));
            ps.AppendLine(Helpers.GetScriptComment("FARM_SPAppSPFeature"));

            if (farmSettings.Servers.Count > 1)
            {
                ps.AppendFormat("\n\n{0} {1}\n", Helpers.GetComment("FARM_RunOnServers"), farmSettings.ServersAsString);
                ps.AppendLine(Helpers.GetScriptComment(Constants.FARM_ConnectSPConfigDB));
            }
            
            return ps.ToString();
        }

        public string GetManagedAccountsScript()
        {
            var ps = new StringBuilder();
            var managedAccountSettings = GetManagedAccounts();

            ps.AppendLine(Helpers.GetComment("MANACC_Heading"));
            ps.Append(Helpers.GetComment("MANACC_Intro"));
            ps.Append(Helpers.GetScriptComment("MANACC_FUN_AddManagedAccount"));

            var addManagedAccount = Helpers.GetScriptComment("MANACC_AddManagedAccount");
            foreach(var account in managedAccountSettings)
            {
                ps.AppendFormat(addManagedAccount, account.DisplayName);
            }

            return ps.ToString();
        }

        public List<string> GetServers()
        {
            var localFarm = SPFarm.Local;
            var serverList = new List<string>();
            foreach (var srv in localFarm.Servers)
            {
                if (string.IsNullOrEmpty(srv.Name) || srv.Role == SPServerRole.Invalid)
                {
                    continue;
                }
                serverList.Add(srv.Name);
            }


            return serverList;
        }
    }
}


